package com.example.baseweb.controller;

import com.example.baseweb.common.ApiResponse;
import com.example.baseweb.common.OrderStatusEnum;
import com.example.baseweb.common.PageResponse;
import com.example.baseweb.dto.OrderInfoDTO;
import com.example.baseweb.entity.OrderInfo;
import com.example.baseweb.service.OrderInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.Map;

@RestController
@RequestMapping("/api/order")
public class OrderInfoController {
    @Autowired
    private OrderInfoService orderInfoService;

    @PostMapping("/page")
    public ApiResponse<PageResponse<OrderInfoDTO>> page(@RequestBody Map<String, Object> params) {
        int page = params.get("page") != null ? Integer.parseInt(params.get("page").toString()) : 1;
        int size = params.get("size") != null ? Integer.parseInt(params.get("size").toString()) : 10;
        String orderNo = params.get("orderNo") != null ? params.get("orderNo").toString() : null;
        String userId = params.get("userId") != null ? params.get("userId").toString() : null;
        String userName = params.get("userName") != null ? params.get("userName").toString() : null;
        String status = params.get("status") != null ? params.get("status").toString() : null;
        String mealType = params.get("mealType") != null ? params.get("mealType").toString() : null;
        String menuDate = params.get("menuDate") != null ? params.get("menuDate").toString() : null;
        String paymentMethod = params.get("paymentMethod") != null ? params.get("paymentMethod").toString() : null;
        
        System.out.println("订单分页查询参数: page=" + page + ", size=" + size + ", orderNo=" + orderNo + ", userId=" + userId + ", userName=" + userName + ", status=" + status + ", mealType=" + mealType + ", menuDate=" + menuDate + ", paymentMethod=" + paymentMethod);
        
        PageResponse<OrderInfoDTO> result = orderInfoService.findOrderPage(page, size, orderNo, userId, userName, status, mealType, menuDate, paymentMethod);
        return ApiResponse.success(result);
    }

    @PostMapping("/detail")
    public ApiResponse<OrderInfoDTO> detail(@RequestBody Map<String, Object> params) {
        Long id = Long.valueOf(params.get("id").toString());
        OrderInfo orderInfo = orderInfoService.findById(id);
        return ApiResponse.success(OrderInfoDTO.fromEntity(orderInfo));
    }

    @PostMapping("/init")
    public ApiResponse<String> initTestData() {
        // 插入一些测试订单数据
        for (int i = 1; i <= 10; i++) {
            OrderInfo order = new OrderInfo();
            order.setOrderNo("20240601" + String.format("%03d", i));
            order.setUserId((long) i);
            order.setUserName("用户" + i);
            order.setOrderDate(LocalDateTime.now().minusDays(i));
            order.setMenuDate(LocalDate.now().minusDays(i));
            order.setMealType(i % 3 == 0 ? "早餐" : (i % 3 == 1 ? "午餐" : "晚餐"));
            order.setDishList("[{\"name\":\"番茄炒蛋\",\"type\":\"主菜\",\"price\":12},{\"name\":\"米饭\",\"type\":\"主食\",\"price\":2}]");
            order.setTotalAmount(new BigDecimal("14.0"));
            order.setStatus(i % 4 == 0 ? "PAID" : (i % 4 == 1 ? "PENDING" : (i % 4 == 2 ? "CANCELLED" : "REFUNDED")));
            
            // 设置支付相关信息
            if ("PAID".equals(order.getStatus())) {
                order.setPaymentMethod("WECHAT");
                order.setPaymentTime(LocalDateTime.now().minusDays(i).plusHours(1));
            }
            
            // 设置退款信息
            if ("REFUNDED".equals(order.getStatus())) {
                order.setRefundTime(LocalDateTime.now().minusDays(i).plusHours(2));
            }
            
            order.setRemark("测试订单备注" + i);
            order.setCreateTime(LocalDateTime.now().minusDays(i));
            order.setUpdateTime(LocalDateTime.now().minusDays(i));
            orderInfoService.save(order);
        }
        return ApiResponse.success("订单测试数据初始化成功");
    }

    @PostMapping("/refund/{orderNo}")
    public ApiResponse<Void> refund(@PathVariable String orderNo) {
        orderInfoService.refundOrder(orderNo);
        return ApiResponse.success();
    }
    
    @PostMapping("/statusDict")
    public ApiResponse<Map<String, String>> getStatusDict() {
        return ApiResponse.success(OrderStatusEnum.getStatusMap());
    }
} 